﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Google.GData;
using Google.GData.Documents;
using Google.GData.Client;

namespace GoogleDocswpfApp
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private Google.GData.Documents.DocumentsService g_doc_service;
        private Google.GData.Documents.DocumentsListQuery g_doc_query_list;
        public Window1()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
            g_doc_service = new Google.GData.Documents.DocumentsService("GoogleDocsWPFSample");

        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            txtUsername.Focus();
        }

        private void buttonSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (txtUsername.Text.Length <= 0 || txtPassword.Password.Length <= 0)
                MessageBox.Show("Username / password cannot be null", "Authentication error", MessageBoxButton.OK, MessageBoxImage.Information);
            else
                authenticateUser(txtUsername.Text, txtPassword.Password);
        }
        private void authenticateUser(string username, string password)
        {
            try
            {
                this.Cursor = Cursors.Wait;
                g_doc_service.setUserCredentials(username, password);
                g_doc_query_list = new DocumentsListQuery();
                g_doc_service.Query(g_doc_query_list);

                UserControl user = new UserControl(g_doc_service);
                Application.Current.MainWindow = user;
                this.Close();
                user.Show();
            }
            catch (AuthenticationException e)
            {
                this.Cursor = Cursors.Arrow;
                MessageBox.Show(e.Message, "Invalid username/password", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            catch (Exception er)
            {
                this.Cursor = Cursors.Arrow;
                MessageBox.Show(er.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            finally
            {
                txtUsername.Focus();
            }

        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                buttonSubmit_Click(sender, e);
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void lblGoogleLink_MouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Diagnostics.Process firefox_process = new System.Diagnostics.Process();
        }
    }
}
